package cn.wolfcode.trip.base.service.impl;

import cn.wolfcode.trip.base.domain.GoodsCollection;
import cn.wolfcode.trip.base.mapper.GoodsCollectionMapper;
import cn.wolfcode.trip.base.service.IGoodsCollectionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class GoodsCollectionServiceImpl implements IGoodsCollectionService {

    @Autowired
    private GoodsCollectionMapper goodsCollectionMapper;
    /**
     * 保存/增加收藏
     */
    @Override
    public void save(GoodsCollection goodsCollection) {
        //查询某一用户对某一产品的收藏记录
        GoodsCollection goodsCollection2 = goodsCollectionMapper.getByAndUserIdCommodityId(goodsCollection.getUser().getId(),goodsCollection.getCommodity().getId());

        if (goodsCollection2==null){
            goodsCollection.setCollection_time(new Date());
            goodsCollection.setCollection_number(1);
            goodsCollectionMapper.insert(goodsCollection);
        }else{
            throw new RuntimeException("已经收藏过了!");
        }
    }

    /**
     * 查询用户的商品收藏
     * @param userId
     * @return
     */
    @Override
    public List<GoodsCollection> getByUserId(Long userId) {
        return goodsCollectionMapper.getByUserId(userId);
    }

}
